package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.base.Result;
import com.lin.entity.Department;
import com.lin.entity.WarningLog;
import com.lin.entity.WarningLogReq;
import com.lin.mapper.WarningLogMapper;
import com.lin.service.WarningDeviceService;
import com.lin.service.WarningLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.util.SmsUtil;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lin
 * @since 2022-06-17
 */
@Service
public class WarningLogServiceImpl extends ServiceImpl<WarningLogMapper, WarningLog> implements WarningLogService {


    @Resource
    private DepartmentServiceImpl departmentService;

    @Override
    public Page<WarningLog> getWarningLogList(WarningLogReq warningLogReq) {
        Page<WarningLog> page = new Page<>(warningLogReq.getPage(), warningLogReq.getSize());
        LambdaQueryWrapper<WarningLog> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(warningLogReq.getDepartmentDid() != 0, WarningLog::getDepartmentDid, warningLogReq.getDepartmentDid())
                .le(WarningLog::getEndTime, warningLogReq.getEndTime())
                .ge(ObjectUtils.isNotEmpty(warningLogReq.getStartTime()), WarningLog::getStartTime, warningLogReq.getStartTime());
        return page(page, queryWrapper);
    }

    @Override
    public Result sendMsgToPhone(String departmentName) {
        //根据部门名称查到手机号
        Department department = departmentService.getOne(new QueryWrapper<Department>().eq("name", departmentName));
        if (Objects.isNull(department)) {
            return Result.fail("未知错误");
        }
        try {
            SmsUtil.sentMsg(department.getPhone());
        } catch (Exception e) {
            System.out.println(e);
        }

        return Result.ok("发送成功");
    }
}
